package com.company.www;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class Worker implements Runnable {

    private CountDownLatch countDownLatch;

    private String name;

    public Worker(CountDownLatch countDownLatch, String name) {
        this.countDownLatch = countDownLatch;
        this.name = name;
    }

    @Override
    public void run() {
        doWork();

        try {
            TimeUnit.SECONDS.sleep(new Random().nextInt(10));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("工人【" + name + "】已完成工作。当前 count = " + countDownLatch.getCount());
        countDownLatch.countDown(); // 计数器减一
    }

    private void doWork() {
        System.out.println("工人【" + name + "】开始准备工作了");
    }
}
